package cn.magic.basic.controller;

import cn.magic.basic.dto.LoginDto;
import cn.magic.basic.exception.BusinessException;
import cn.magic.basic.service.ILoginService;
import cn.magic.basic.utils.AjaxResult;

import cn.magic.user.dto.WxDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;


    /** 
    * @Title: account
    * @Description: 根据账号密码登录
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/7 15:12  
    * @Parameters: [loginDto]
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
           Map<String,Object> map = loginService.account(loginDto);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }


    /**
    * @Title: quit
    * @Description: 退出登录
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/9 13:05
    * @Parameters: [request]
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @GetMapping("/quit")
    public AjaxResult quit(HttpServletRequest request){
        try {
           loginService.quit(request);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }

    /**
    * @Title: sendMobileCode
    * @Description: 发送验证码
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/9 17:00
    * @Parameters: [loginDto]
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @PostMapping("/sendMobileCode")
    public AjaxResult sendMobileCode(@RequestBody LoginDto loginDto){
        try {
            loginService.sendMobileCode(loginDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }


    /**
    * @Title: codeLogin
    * @Description: TODO
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/9 18:23
    * @Parameters: [loginDto]
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @PostMapping("/codeLogin")
    public AjaxResult codeLogin(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = loginService.codeLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }

   

    /** 
    * @Title: wechat
    * @Description: 微信登录接口
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/10 17:10  
    * @Parameters: []
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WxDto wxDto){
        try {
            return loginService.wechat(wxDto);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }


    /** 
    * @Title: binder
    * @Description: 微信登录绑定账号
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/10 18:53  
    * @Parameters: [wxDto]
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WxDto wxDto){
        try {
            Map<String,Object> map = loginService.binder(wxDto);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }



}
